﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.BLL;
using rock.UI.common;

namespace rock.UI
{
    public partial class FormCfgCodeValue : Form
    {
        private ModelCFG_CODE mcc = new ModelCFG_CODE();
        public ModelCFG_CODE _MCC
		{
            set { mcc = value; }
            get { return mcc; }
		}


        DalCFG_CODE dal = new DalCFG_CODE();

        public FormCfgCodeValue()
        {
            InitializeComponent();

            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private void FormCfgCodeValue_Load(object sender, EventArgs e)
        {
            if (this._MCC.CODE_VALUE != null && !this._MCC.CODE_VALUE.Equals("")) {
                this.textBox1.Text = this._MCC.CODE_VALUE;
            }
            this.groupBox1.Text = this._MCC.TYPE;
            //this.userControlCommonLeftInfoPanel1.labelHeaderInfo.Text = "代码库设置";
            //this.userControlCommonLeftInfoPanel1.textBoxBodyInfo.Text = "用于预先录入在软件的其他模块中可以选择输入的常用项目,如房屋朝向,房屋结构,楼宇类型,开发商,户型,设备等";
        }


        private Boolean check() {
            try
            {
                StringUtils.checkText(this.textBox1.Text, 2, 30, "名称");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            //更新.原值不检测
            if(this._MCC.SEQID > 0 )
            {
                if (mcc.CODE_VALUE.Equals(this.textBox1.Text.Trim())) {
                    return true;
                }
            }
            String existSql = "select count(1) from CFG_CODE where TYPE = '{0}' and CODE_VALUE = '{1}'";
            existSql = String.Format(existSql,this._MCC.TYPE , this.textBox1.Text);
            if (BllCommonUtil.Exists(existSql))
            {
                MessageBox.Show("此名称值已存在在,请输入其它的值", "提示");
                return false;
            }

            return true;
        
        }
             

        private void button1_Click(object sender, EventArgs e)
        {

            if (!check())
            {
                return;
            }

             
            mcc.CODE_VALUE = this.textBox1.Text;
            if ( mcc.SEQID <= 0)
            {
                dal.Add(mcc);
            }
            else {
                dal.Update(mcc);
            }
            this.Close();
            //触发代理
            oneRefreshParentData();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        //代理
        public delegate  void  RefreshParentData();
        public RefreshParentData oneRefreshParentData;
    }
}
